from django.contrib import admin

from backend.constants import (
    TIER1, TIER2, TIER3, TIER4
)
from posts.models import (
    Post,
    ANNOTATION_TIER1,
    ANNOTATION_TIER2,
    ANNOTATION_TIER3,
    ANNOTATION_TIER4
)


@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
    list_display = ('id', 'title', 'author', 'category', 'likes',
                    'created_at', 'annotation'
                    )
    list_filter = ('annotation',)

    actions = ['set_tier1', 'set_tier2', 'set_tier3', 'set_tier4']

    readonly_fields = ('created_at', 'tags')
    date_hierarchy = 'created_at'

    search_fields = ('id', 'title', 'author__username', 'author__first_name',
                     'author__last_name', 'author__email',
                     'author__qq', 'author__phone', 'annotation',
                     )
    show_full_result_count = False

    @admin.action(description='设为{0}({1}级)'.format(ANNOTATION_TIER1, TIER1))
    def set_tier1(self, request, queryset):
        queryset.update(annotation=TIER1)

    @admin.action(description='设为{0}({1}级)'.format(ANNOTATION_TIER2, TIER2))
    def set_tier2(self, request, queryset):
        queryset.update(annotation=TIER2)

    @admin.action(description='设为{0}({1}级)'.format(ANNOTATION_TIER3, TIER3))
    def set_tier3(self, request, queryset):
        queryset.update(annotation=TIER3)

    @admin.action(description='设为{0}({1}级)'.format(ANNOTATION_TIER4, TIER4))
    def set_tier4(self, request, queryset):
        queryset.update(annotation=TIER4)
